package dreamCookGame.chickenMiniGame;

import java.awt.image.BufferedImage;

import dreamCookGame.Item;
import dreamCookGame.References;

public class Entity{
	public Vector _location;
	public Vector _velocity;
	String _name;
	public BufferedImage[] _sprite;
	public float _currentFrame;
	double _direction;
	int _totalFrames;
	float _horizontalVelocity;
	float _verticalVelocity;
	float _accelerationRate;
	float _friction;
	float _maxSpeed;
	
	Entity(String name, String[] stateImageName){
		//super(name, stateImageName, 0, 0, null, null, 0);
		_name = name;
		_location = new Vector(0,0);
		_velocity = new Vector(0,0);
		_currentFrame = 0;
		_direction = Math.random()*Math.PI;
		_horizontalVelocity = 0;
		_verticalVelocity = 0;
		_accelerationRate = 0;
		_friction = 0;
		_maxSpeed = 100;
	}
	public void loadResources(References reference){
		//super.loadResources(reference);
		_sprite = reference.getImageArray(_name);
		_totalFrames = _sprite.length;
	}
	public void update(){

		_velocity.setX(_horizontalVelocity);
		_velocity.setY(_verticalVelocity);
		if(_velocity.getSquareMagnitude()>_maxSpeed*_maxSpeed){
			_velocity.normalize();
			_velocity.scale(_maxSpeed);
		}
		_location.add(_velocity);
		_currentFrame += _velocity.getMagnitude()/30;
		if(_currentFrame>=_totalFrames){
			_currentFrame = 0;
		}
		if(_velocity.getSquareMagnitude()>2){
			_direction = _velocity.getDirection();
		}
		applyFriction();
		//super.updatePosition((int)_location.getX()-64, (int)_location.getY()-64);
	}
	public void setLocation(float x, float y){
		_location = new Vector(x,y);
		//super.updatePosition((int)_location.getX()-64, (int)_location.getY()-64);
	}
	public void setLocation(Vector v){
		_location = v;
		//super.updatePosition((int)_location.getX()-64, (int)_location.getY()-64);
	}
	public void setVelocity(float vx, float vy){
		_velocity = new Vector(vx,vy);
		_horizontalVelocity = _velocity.getX();
		_verticalVelocity = _velocity.getY();
	}
	public void setVelocity(Vector v){
		_velocity = v;
		_horizontalVelocity = _velocity.getX();
		_verticalVelocity = _velocity.getY();
	}
	public void setHorizontalVelocity(float horizontalVelocity){
		_horizontalVelocity = horizontalVelocity;
		_velocity.setX(horizontalVelocity);
	}
	public void setVerticalVelocity(float verticalVelocity){
		_verticalVelocity = verticalVelocity;
		_velocity.setY(verticalVelocity);
	}
	private void applyFriction() {
		float mag = _velocity.getMagnitude();
		if(mag<_friction){
			_velocity.set(0, 0);
			setVelocity(_velocity);
		}else{
			_velocity.setMagnitude(mag-_friction);
			setVelocity(_velocity);
		}
	}
	public void setAccelerationRate(float accelerationRate){
		_accelerationRate = accelerationRate;
	}
	public void setMaxSpeed(float maxSpeed){
		_maxSpeed = maxSpeed;
	}
	public void setFriction(float friction){
		_friction = friction;
	}
	public double getDirection(){
		return _direction;
	}
}